﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using One2Team.EasyScrum.UI.Web.Models;

namespace One2Team.EasyScrum.UI.Web.Controllers
{
    public class SprintController : Controller
    {
        private easyscrumEntities db = new easyscrumEntities();

        //
        // GET: /Sprint/

        public ActionResult Index()
        {
            var sprint = db.sprint.Include(s => s.project).Include(s => s.sprint_backlog);
            return View(sprint.ToList());
        }

        //
        // GET: /Sprint/Details/5

        public ActionResult Details(int id = 0)
        {
            sprint sprint = db.sprint.Find(id);
            if (sprint == null)
            {
                return HttpNotFound();
            }
            return View(sprint);
        }

        //
        // GET: /Sprint/Create

        public ActionResult Create()
        {
            ViewBag.id_project = new SelectList(db.project, "id", "nom");
            //ViewBag.id_sprint_backlog = new SelectList(db.sprint_backlog, "id", "nom");
            return View();
        }

        //
        // POST: /Sprint/Create

        [HttpPost]
        public ActionResult Create(sprint sprint)
        {
            if (ModelState.IsValid)
            {
                // On créer un SprintBacklog et on le lie au sprint.
                var sb = new sprint_backlog();
                sb.nom = "Sprint_Backlog_" + sprint.nom;
                sprint.sprint_backlog = sb;

                db.sprint.Add(sprint);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            ViewBag.id_project = new SelectList(db.project, "id", "nom", sprint.id_project);
            ViewBag.id_sprint_backlog = new SelectList(db.sprint_backlog, "id", "nom", sprint.id_sprint_backlog);
            return View(sprint);
        }

        //
        // GET: /Sprint/Edit/5

        public ActionResult Edit(int id = 0)
        {
            sprint sprint = db.sprint.Find(id);
            if (sprint == null)
            {
                return HttpNotFound();
            }
            ViewBag.id_project = new SelectList(db.project, "id", "nom", sprint.id_project);
            ViewBag.id_sprint_backlog = new SelectList(db.sprint_backlog, "id", "nom", sprint.id_sprint_backlog);
            return View(sprint);
        }

        //
        // POST: /Sprint/Edit/5

        [HttpPost]
        public ActionResult Edit(sprint sprint)
        {
            if (ModelState.IsValid)
            {
                db.Entry(sprint).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.id_project = new SelectList(db.project, "id", "nom", sprint.id_project);
            ViewBag.id_sprint_backlog = new SelectList(db.sprint_backlog, "id", "nom", sprint.id_sprint_backlog);
            return View(sprint);
        }

        //
        // GET: /Sprint/Delete/5

        public ActionResult Delete(int id = 0)
        {
            sprint sprint = db.sprint.Find(id);
            if (sprint == null)
            {
                return HttpNotFound();
            }
            return View(sprint);
        }

        //
        // POST: /Sprint/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            sprint sprint = db.sprint.Find(id);
            db.sprint.Remove(sprint);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}